//
//  ProgramTableViewController.m
//  To Feel
//
//  Created by 于成哲 on 16/7/23.
//  Copyright © 2016年 caoyu. All rights reserved.
//

#import "ProgramTableViewController.h"
#import "CellOfProgram.h"
#import "NetworkingHandler.h"
#import "ModelOfProgram.h"
#import "UIImageView+WebCache.h"
#import "Masonry.h"
#import "SJ_AudioPlayer.h"
@interface ProgramTableViewController ()
@property (nonatomic, retain) NSMutableArray *arrOfProgram;

@property (nonatomic, retain) NSDictionary *dicOfDetail;

@property (nonatomic, retain) NSMutableArray *arrOfDetail;

@property (nonatomic, retain) NSMutableArray *arrOfAudio;

@property (nonatomic, retain) NSMutableArray *arrOfImage;

@property (nonatomic, retain) UIImageView *imagePic;

@property (nonatomic, retain) UILabel *labelOfTitle;

@property (nonatomic, retain) UILabel *labelOfType;

@property (nonatomic, retain) UILabel *labelOfCount;

@property (nonatomic, retain) UILabel *labelOfName;

@property (nonatomic, retain) UIButton *buttonOfTake;

@property (nonatomic, retain) UIButton *buttonOfDownLoad;
@end

@implementation ProgramTableViewController
- (instancetype)initWithStyle:(UITableViewStyle)style {
    
    self = [super initWithStyle:style];
    
    if (self) {
        
//        [self handleData];
    }
    
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"专辑详情";
    
//    [self configNavigationBar];
    
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
//    self.tabBarController.tabBar.hidden = YES;
    
    self.navigationController.navigationBar.hidden = NO;
    
    [self.tableView registerClass:[CellOfProgram class] forCellReuseIdentifier:@"pool"];
}
- (void)setUrlString:(NSString *)urlString {
    
    if (_urlString != urlString) {
        _urlString = urlString;
    }
    [self handleData];
}
- (void)handleData {
    
    NetworkingHandler *nt = [[NetworkingHandler alloc] init];
    
//    NSString *str = @"http://mobile.ximalaya.com/mobile/v1/album?albumId=3224880&device=android&isAsc=true&pageId=1&pageSize=20&pre_page=0&source=0&statEvent=pageview%2Falbum%403224880&statModule=%E5%90%AC%E5%A4%A7%E8%BF%9E&statPage=tab%40%E5%8F%91%E7%8E%B0_%E6%8E%A8%E8%8D%90&statPosition=2";
    
    self.dicOfDetail = [NSDictionary dictionary];
    
    self.arrOfProgram = [NSMutableArray array];
    self.arrOfAudio = [NSMutableArray array];
    self.arrOfImage = [NSMutableArray array];
    [nt netWorkingHandlerGETWithURL:self.urlString
                         completion:^(id result, NSData *data, NSURLResponse *response, NSError *error) {
        
        NSDictionary *dic = [result objectForKey:@"data"];
        
        self.dicOfDetail = [dic objectForKey:@"album"];
        
        NSDictionary *dic1 = [dic objectForKey:@"tracks"];
        
        NSArray *arr = [dic1 objectForKey:@"list"];
        
        for (NSDictionary *dic2 in arr) {
            
            ModelOfProgram *model = [[ModelOfProgram alloc] init];
            
            [model setValuesForKeysWithDictionary:dic2];
            
            [self.arrOfProgram addObject:model];
            [self.arrOfAudio addObject:[NSURL URLWithString:model.playUrl64]];
            [self.arrOfImage addObject:[NSURL URLWithString:model.coverMiddle]];
            
            [self.tableView reloadData];
        }
        
        [self createHeadView];
    }];
}

- (void)configNavigationBar {
    
//    UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"zhuanzai.png"] style:UIBarButtonItemStylePlain target:self action:@selector(handleNext)];
//    
//    UIBarButtonItem *dian = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"dian.png"] style:UIBarButtonItemStylePlain target:self action:@selector(handleDian)];
    
//    self.navigationItem.rightBarButtonItems = @[dian, next];
}

- (void)createHeadView {
    
    UIView *viewOfHead = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    
    self.tableView.tableHeaderView = viewOfHead;
    
    self.imagePic = [[UIImageView alloc] init];
    [viewOfHead addSubview:self.imagePic];
    
    self.labelOfTitle = [[UILabel alloc] init];
    [viewOfHead addSubview:self.labelOfTitle];
    
    self.labelOfName = [[UILabel alloc] init];
    [viewOfHead addSubview:self.labelOfName];
    self.labelOfName.font = [UIFont systemFontOfSize:14];
    
    self.labelOfCount = [[UILabel alloc] init];
    [viewOfHead addSubview:self.labelOfCount];
    self.labelOfCount.font = [UIFont systemFontOfSize:14];
    
    self.labelOfType = [[UILabel alloc] init];
    [viewOfHead addSubview:self.labelOfType];
    self.labelOfType.font = [UIFont systemFontOfSize:14];
    
//    self.buttonOfTake = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.buttonOfTake.layer.cornerRadius = 5;
//    self.buttonOfTake.titleLabel.textAlignment = NSTextAlignmentCenter;
//    [self.buttonOfTake setBackgroundColor:[UIColor greenColor]];
//    [self.buttonOfTake setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [viewOfHead addSubview:self.buttonOfTake];
    
//    self.buttonOfDownLoad = [UIButton buttonWithType:UIButtonTypeCustom];
//    [viewOfHead addSubview:self.buttonOfDownLoad];
//    self.buttonOfDownLoad.layer.cornerRadius = 5;
//    self.buttonOfDownLoad.layer.borderWidth = 1;
//    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
//    CGColorRef borderColorRef = CGColorCreate(colorSpace,(CGFloat[]){0, 1, 0, 1});
//    self.buttonOfDownLoad.layer.borderColor = borderColorRef;
//    self.buttonOfDownLoad.titleLabel.textAlignment = NSTextAlignmentCenter;
//    [self.buttonOfDownLoad setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    
    [self.imagePic mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(viewOfHead).with.offset(30);
        make.left.equalTo(viewOfHead).with.offset(30);
        make.width.mas_equalTo((CGRectGetHeight(viewOfHead.bounds) - 30));
        make.height.mas_equalTo((CGRectGetHeight(viewOfHead.bounds) - 30));
    }];
    
    [self.labelOfTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(viewOfHead).with.offset(30);
        make.left.equalTo(self.imagePic.mas_right).with.offset(30);
        make.right.equalTo(viewOfHead).with.offset(-30);
        make.height.mas_equalTo(((CGRectGetHeight(viewOfHead.bounds) - 30)  - 50) / 4);
    }];
    
    [self.labelOfName mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.labelOfTitle.mas_bottom).with.offset(10);
        make.left.equalTo(self.imagePic.mas_right).with.offset(30);
        make.right.equalTo(viewOfHead).with.offset(-30);
        make.height.mas_equalTo(((CGRectGetHeight(viewOfHead.bounds) - 30) - 50) / 4);
    }];
    
    [self.labelOfCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.labelOfName.mas_bottom).with.offset(10);
        make.left.equalTo(self.imagePic.mas_right).with.offset(30);
        make.right.equalTo(viewOfHead).with.offset(-30);
        make.height.mas_equalTo(((CGRectGetHeight(viewOfHead.bounds) - 30) - 50) / 4);
        
    }];
    
    [self.labelOfType mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.labelOfCount.mas_bottom).with.offset(10);
        make.right.equalTo(viewOfHead).with.offset(-30);
        make.left.equalTo(self.imagePic.mas_right).with.offset(30);
        make.height.mas_equalTo(((CGRectGetHeight(viewOfHead.bounds) - 30) - 50) / 4);
        
    }];
    
    [self.buttonOfTake mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(viewOfHead).with.offset(30);
        make.top.equalTo(self.imagePic.mas_bottom).with.offset((CGRectGetHeight(viewOfHead.bounds) - 30) / 2 / 3);
        make.height.mas_equalTo((CGRectGetHeight(viewOfHead.bounds) - 30) / 2 / 3);
        make.width.mas_equalTo((SCREEN_WIDTH - 90) / 2);
    }];
    
    [self.buttonOfDownLoad mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.imagePic.mas_bottom).with.offset((CGRectGetHeight(viewOfHead.bounds) - 30) / 2 / 3);
        make.height.mas_equalTo((CGRectGetHeight(viewOfHead.bounds) - 30) / 2 / 3);
        make.right.equalTo(viewOfHead).with.offset(-30);
        make.left.equalTo(self.buttonOfTake.mas_right).with.offset(30);
    }];
    
    [self configHeadView];
}

- (void)configHeadView {
    
    [self.imagePic sd_setImageWithURL:[NSURL URLWithString:self.dicOfDetail[@"coverMiddle"]]];
    
    self.labelOfTitle.text = self.dicOfDetail[@"title"];
    
    self.labelOfName.text = [NSString stringWithFormat:@"主播: %@", self.dicOfDetail[@"nickname"]];
    
    
    NSString *str = self.dicOfDetail[@"playTimes"];
    int a = [str intValue];
    CGFloat count = a / 10000.0;
    self.labelOfCount.text = [NSString stringWithFormat:@"播放: %.1lf万", count];
    
    self.labelOfType.text = [NSString stringWithFormat:@"分类: %@", self.dicOfDetail[@"categoryName"]];
    
    [self.buttonOfTake setTitle:@"订阅专辑" forState:UIControlStateNormal];
    
    [self.buttonOfDownLoad setTitle:@"批量下载" forState:UIControlStateNormal];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.arrOfProgram.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CellOfProgram *cell = [tableView dequeueReusableCellWithIdentifier:@"pool" forIndexPath:indexPath];
    
    cell.model = [self.arrOfProgram objectAtIndex:indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return (SCREEN_HEIGHT - 64 - 50) / 5;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SJ_AudioPlayer *audio = [[SJ_AudioPlayer alloc]initWithNibName:nil bundle:nil arrayOfUrl:self.arrOfAudio];
    audio.arrOfImage = self.arrOfImage;
    audio.currentPlayIndex = indexPath.row;
    
    [self.navigationController pushViewController:audio animated:YES];
    
}
@end
